#!/usr/bin/perl -w
################################################################################
#
#  mktodo -- generate baseline and todo files by running mktodo.pl
#
# It calls plain 'mktodo' on each perl version it finds based on the input
# parameters.
#
################################################################################
#
#  Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
#  Version 2.x, Copyright (C) 2001, Paul Marquess.
#  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the same terms as Perl itself.
#
################################################################################

use strict;
use Getopt::Long;

require './devel/devtools.pl';
require './parts/ppptools.pl';

our %opt = (
  base    => 0,     # If specified, this will generate base files, not todo ones
  check   => 1,     # Do extra checking
  verbose => 0,
  install => '/tmp/perl/install/default',
  blead   => 'bleadperl-debug',
  debug   => 0,
 'debug-start' => "",   # build an incomplete output, starting with the
                        # specified perl of the form perl5.xxxyyy
);

# The way this works, is it expects to find perl binaries for a bunch of
# different versions in a given directory.  This defaults to the 'install' one
# listed above, but is overriddable by the --install parameter.  Comma
# separating --install allows multiple source directories.
# It also uses blead, again with an overridable default.
#
# It first verifies that the test file works properly for blead.
#
# Then it goes through the list of perl binaries sorted in decreasing order of
# version number.  If something works in version n, but not in version n-1,
# that means it was introduced (or perhaps fixed) in version n, and adds that
# thing to the version n list.
#
# After everything is done, we have lists of what got added when.  The --base
# parameter tells it to not use ppport.h when computing this.  Thus we get
# what the official perls added when.  Without this parameter, we do use
# ppport.h, so we get, as patched by ppport.h, what gets added when

GetOptions(\%opt, qw( base check! verbose install=s blead=s blead-version=s
                      debug=i debug-start=s)) or die;

identify();

my $perls_ref = get_and_sort_perls(\%opt);

# Go through all the perls, creating a todo file for it.
for (my $i = 0; $i < @$perls_ref; $i++) {
  my $this_perl = @{$perls_ref}[$i];
  my @args = ('--perl',     $this_perl->{path},
              '--version',  $this_perl->{version},
              '--todo-dir', (($opt{base}) ? 'parts/base' : 'parts/todo')
             );

  push @args, '--blead' if $i == 0; # First one is blead
  push @args, '--todo', $this_perl->{'todo'};
  push @args, '--base' if $opt{base};
  push @args, "--debug=$opt{debug}" if $opt{debug};
  push @args, '--verbose' if $opt{verbose};
  push @args, '--nocheck' unless $opt{check};
  push @args, '--final', $this_perl->{'final'} if $this_perl->{'final'};
  runperl('devel/mktodo.pl', @args) or die "error running mktodo.pl [$!] [$?]\n";
}
